# Multiple plot function
#
# ggplot objects can be passed in ..., or to plotlist (as a list of ggplot objects)
# - cols:   Number of columns in layout
# - layout: A matrix specifying the layout. If present, 'cols' is ignored.
#
# If the layout is something like matrix(c(1,2,3,3), nrow=2, byrow=TRUE),
# then plot 1 will go in the upper left, 2 will go in the upper right, and
# 3 will go all the way across the bottom.
#
multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
  require(grid)

  # Make a list from the ... arguments and plotlist
  plots <- c(list(...), plotlist)

  numPlots = length(plots)

  # If layout is NULL, then use 'cols' to determine layout
  if (is.null(layout)) {
    # Make the panel
    # ncol: Number of columns of plots
    # nrow: Number of rows needed, calculated from # of cols
    layout <- matrix(seq(1, cols * ceiling(numPlots/cols)),
                    ncol = cols, nrow = ceiling(numPlots/cols))
  }

 if (numPlots==1) {
    print(plots[[1]])

  } else {
    # Set up the page
    grid.newpage()
    pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))

    # Make each plot, in the correct location
    for (i in 1:numPlots) {
      # Get the i,j matrix positions of the regions that contain this subplot
      matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))

      print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,
                                      layout.pos.col = matchidx$col))
    }
  }
}

require(ggplot2)
require(mgcv)
require(multiplot)
library(ggplot2)

national<-read.dta(file="Fig-data_figure3.dta")

p1<-(ggplot(data=national, aes(x=year, y=govislib))
     +scale_x_continuous(limits = c(1972, 2012), breaks=c(1972, 1982, 1992, 2002, 2012))
     + geom_point()
     + theme(axis.title.x=element_text(size=12))
     + theme(axis.title.y=element_text(size=12))
     + geom_line(size=1.5) 
     + ylab("Policy Liberalism (govislib)")
     +geom_errorbar(data = national, aes(ymin = govislib - govislib_se, ymax = govislib + govislib_se, width=.25),colour = "black")+    ylab("Policy Liberalism")
     + theme_bw()
     + xlab("year"))
limits <- aes(ymax = mood + mood_se, ymin=mood - mood_se, data=national)
p2<-(ggplot(data=national, aes(x=year, y=mood))
     +scale_x_continuous(limits = c(1972, 2012), breaks=c(1972, 1982, 1992, 2002, 2012))
     +scale_y_continuous(breaks=c(50, 55, 60, 65, 70),labels=c("50  ", "55  ", "60  ", "65  ", "70  "))
     + geom_point()
     + theme(axis.title.x=element_text(size=12))
     + theme(axis.title.y=element_text(size=12))
     + geom_line(size=1.5) 
     +geom_errorbar(data = national, aes(ymin = mood - mood_se, ymax = mood + mood_se, width=.25),colour = "black")+    ylab("Mood")
     + theme_bw()

     + xlab("year"))	
        
pdf(file="Fig-figure3.pdf", width=8, height=7.5)
multiplot( p1, p2)
    dev.off()
